package com.gdepri.mqtt.zpublisher;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.gdepri.mqtt.config.Config;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import static java.lang.Thread.sleep;

//用的是org.eclipse.paho.client.mqttv3.*;
//@Component
public class PublishSampleV2_1 {

//    public static void main(String[] args) {

//    @Autowired
    private Config config;

    @PostConstruct
     public  void Main() throws InterruptedException {
        sleep(3000);
        JSONObject obj1 = new JSONObject();
        obj1.put("nodeId", "T651GW202306010007000002_chs1");
        obj1.put("name", "T651CHS1");
        obj1.put("description", "");
        obj1.put("manufacturerId", "GZPR");
        obj1.put("model", "GZPR_CHS_GW");

        JSONObject obj2 = new JSONObject();
        obj2.put("nodeId", "T651GW202306010007000002_acc1");
        obj2.put("name", "T651ACC1");
        obj2.put("description", "");
        obj2.put("manufacturerId", "GZPR");
        obj2.put("model", "GZPR_ACC_CS");

        JSONArray deviceInfos = new JSONArray();
        deviceInfos.add(obj1);
        deviceInfos.add(obj2);

        JSONObject obj = new JSONObject();
        obj.put("mid",0);
        obj.put("deviceInfos", deviceInfos);

        String content = obj.toJSONString();

        String broker = config.getPublishBroker();
        String topic = config.getPublishTopic();
        String username = config.getPublishUsername();
        String password = config.getPublishPassword();
        String clientid = config.getPublishClientId();

        int qos = 2;

        try {
            MqttClient client = new MqttClient(broker, clientid, new MemoryPersistence());
            // 连接参数
            MqttConnectOptions options = new MqttConnectOptions();
            // 设置用户名和密码
            options.setUserName(username);
            options.setPassword(password.toCharArray());
            options.setKeepAliveInterval(60);
            options.setConnectionTimeout(60);
            // 连接
            client.connect(options);
            // 创建消息并设置 QoS
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            // 发布消息
            client.publish(topic, message);
            System.out.println("Message published");
            System.out.println("topic: " + topic);
            System.out.println("message content: " + content);
            // 断开连接
            client.disconnect();
            // 关闭客户端
            client.close();
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }

    }
}
